//
//  NewFeatureViewController.swift
//  Status
//
//  Created by 花轮 on 15/5/14.
//  Copyright (c) 2015年 花轮. All rights reserved.
//

import UIKit


class NewFeatureViewController: UICollectionViewController {

    @IBOutlet weak var layout: UICollectionViewFlowLayout!
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        layout.itemSize = view.bounds.size
    }
    
    // MARK: UICollectionViewDataSource
    let imageCount = 4
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //#warning Incomplete method implementation -- Return the number of items in the section
        return imageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("newCell", forIndexPath: indexPath) as! NewFeatureCell
    
        // Configure the cell
        cell.imageIndex = indexPath.item
        
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
//        println(collectionView.indexPathsForVisibleItems())
        
        let path = collectionView.indexPathsForVisibleItems().last as! NSIndexPath
        
        if path.item == imageCount - 1
        {
            let cell = collectionView.cellForItemAtIndexPath(path) as! NewFeatureCell
            
            cell.showStartBtn()
        }
    }
}

class NewFeatureCell: UICollectionViewCell {
    
    @IBOutlet weak var startButton: UIButton!
    @IBOutlet weak var iconView: UIImageView!
    
    var imageIndex: Int = 0{
        didSet{
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            
            startButton.hidden = true
        }
    }
    
    func showStartBtn()
    {
        startButton.hidden = false
        
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1.2, animations: { () -> Void in
            self.startButton.transform = CGAffineTransformMakeScale(1.0, 1.0)
        })
    }
    
    @IBAction func startClick() {
        NSNotificationCenter.defaultCenter().postNotificationName(LKSwitchRootViewControllerNotification, object: "Main")
    }
    
}
